package com.air;

import com.air.dao.AircraftsMapper;
import com.air.dao.AirportMapper;
import com.air.dao.CityMapper;
import com.air.dao.FlightMapper;
import com.air.entity.Aircrafts;
import com.air.entity.Airport;
import com.air.entity.City;
import com.air.entity.Flight;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Random;


@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = "classpath:applicationContext.xml")
public class gcgjnhkvbhjk {

    @Test
    public void create() throws ParseException {
        //要生成哪天的数据
        mm(21);
    }

    private Date date1;
    private Date date2;
    @Autowired
    CityMapper cityMapper;

    @Autowired
    AircraftsMapper aircraftsMapper;

    @Autowired
    FlightMapper flightMapper;

    @Autowired
    AirportMapper airportMapper;

    public void mm(int day) throws ParseException {

        List<City> cities = cityMapper.selectCity();
        int cSize = cities.size();

        List<Aircrafts> aircrafts = aircraftsMapper.getList();
        int acSize = aircrafts.size();

        List<Airport> airports = airportMapper.getList();
        int apSize = airports.size();

        System.out.println(aircrafts);


        List<Flight> flights = new ArrayList<>();

        for (int t = 0; t < cSize ;t++){
            for(int l = 0; l < cSize; l++){

                if (t == l){
                    continue;
                }else{
                    for (Airport a: airports) {
                        int cityId = (int)a.getCityId();
                        if (cityId == t){
                            for (Airport b: airports) {
                                int cityIdb = (int)b.getCityId();
                                if (cityIdb == l){

                                    Flight flight = new Flight();
                                    flight.setTakeOffCity(cityId);
                                    flight.setLandingCity(cityIdb);
                                    jj(day);
                                    flight.setTakeOffTime(date1);
                                    flight.setLandingTime(date2);
                                    int ec = (int) (Math.random() * (2000 - 500)) +500;
                                    int fc = (int) (Math.random() * (5000 - 2000)) +2000;
                                    flight.setEconomyClass(ec);
                                    flight.setFirstClass(fc);
                                    flight.setEconomyClassSize(132);
                                    flight.setFirstClassSize(23);
                                    int ti = (int) (Math.random() * (7 - 1)) +1;
                                    flight.setAircraftsId(ti);
                                    flight.setTakeOffAirport((int)a.getId());
                                    flight.setName(getRandomString(2)+getRandomInt(4));
                                    flight.setLandingAirport((int)b.getId());
                                    flights.add(flight);


                                }
                            }

                        }
                    }
                }

            }
        }

        for (Flight ffff:
        flights) {
            System.out.println(ffff);
        }

        flightMapper.insert(flights);


    }

    public void jj(int day) throws ParseException {
        Date date = new Date();
        String s = new SimpleDateFormat("YYYY-MM-").format(new Date());
        String s1 =s;
        String s2 =s;

        String dd = String.valueOf(day);
        int hh = (int) (Math.random() * (23 - 0)) +0; //m*~*n
        int mm = (int) (Math.random() * (59 - 0)) +0; //m*~*n
        int ss = (int) (Math.random() * (59 - 0)) +0; //m*~*n


        int hhh = (int) (Math.random() * (5 - 0)) +0; //m*~*n
        int mmm = (int) (Math.random() * (59 - 0)) +0; //m*~*n

        s1 = s1 + dd;
        s1 =s1 + " ";
        if (hh <10){
            s1 = s1 + "0" + hh;
        }else{
            s1 = s1 + hh;
        }
        s1 =s1 + "-";
        if (mm < 10){
            s1 = s1 + "0" + mm;
        }else{
            s1 = s1 + mm;
        }
        s1 =s1 + "-";
        if (ss < 10){
            s1 = s1 + "0" + ss;
        }else{
            s1 = s1 + ss;
        }
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH-mm-ss");
        date1 = formatter.parse(s1);
        System.out.println(date1);


        int ssss=0;
        int mmmm =0;
        int hhhh = 0;
        if (ss < 10){
            ssss =ss;
        }else{
            ssss =ss;
        }

        if (mm+mmm < 10){
            mmmm =  (mm+mmm);
        }else{
            if (mm+mmm >59){
                hh ++;
                mmmm = mm+mmm-60;
            }else{

                mmmm =  mm+mmm;
            }
        }

        if (hh+hhh <10){
            hhhh =  (hh+hhh);
        }else{
            if (hh+hhh >23){
                int i = Integer.parseInt(dd);
                i++;
                dd = String.valueOf(i);
                hhhh  = hh+hhh;
            }else{
                hhhh =hh+hhh;
            }
        }
        s2 = s2 + dd+" "+hhhh+"-"+mmmm+"-"+ssss;


        SimpleDateFormat formatter2 = new SimpleDateFormat("yyyy-MM-dd HH-mm-ss");
        date2 = formatter2.parse(s2);
        //System.out.println(date2);
    }

    public static String getRandomString(int length){

        String str="QWERTYUIOPASDFGHJKLZXCVBNM";

        //由Random生成随机数

        Random random=new Random();

        StringBuffer sb=new StringBuffer();


        for(int i=0; i<length; ++i){


            int number=random.nextInt(26);

            //将产生的数字通过length次承载到sb中

            sb.append(str.charAt(number));

        }

        return sb.toString();

    }

    public static String getRandomInt(int length){

        String str="0123456789";

        //由Random生成随机数

        Random random=new Random();

        StringBuffer sb=new StringBuffer();


        for(int i=0; i<length; ++i){


            int number=random.nextInt(10);

            //将产生的数字通过length次承载到sb中

            sb.append(str.charAt(number));

        }

        return sb.toString();

    }

}
